import { Module } from 'vuex';
import { IRootState } from '../../types';
import { IDashBoardState } from './dashboard-type';

import {
  requestCategoryGoodsCount,
  requestCategoryGoodsSale,
  requestCategoryGoodsFavor,
  requestAddressGoodsSale,
  getAmountList
} from '@/service/analysis/dashboard';

const dashBoardModule: Module<IDashBoardState, IRootState> = {
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [],
      categoryGoodsFavor: [],
      categoryGoodsSale: [],
      addressGoodsSale: [],
      topPanelDatas: []
    };
  },
  mutations: {
    changeTopPanelDatas(state, payload) {
      state.topPanelDatas = payload;
    },
    changeCategoryGoodsCount(state, payload) {
      state.categoryGoodsCount = payload;
    },
    changeCategoryGoodsFavor(state, payload) {
      state.categoryGoodsFavor = payload;
    },
    changeCategoryGoodsSale(state, payload) {
      state.categoryGoodsSale = payload;
    },
    changeAddressGoodsSale(state, payload) {
      state.addressGoodsSale = payload;
    }
  },
  actions: {
    async getDashboardDataAction({ commit }) {
      const topPanelDatasResult = await getAmountList();
      const goodsCountResult = await requestCategoryGoodsCount();
      const addressGoodsSaleResult = await requestAddressGoodsSale();
      const goodsSaleResult = await requestCategoryGoodsSale();
      const goodsFavorResult = await requestCategoryGoodsFavor();
      commit('changeCategoryGoodsCount', goodsCountResult.data);
      commit('changeCategoryGoodsFavor', goodsFavorResult.data);
      commit('changeCategoryGoodsSale', goodsSaleResult.data);
      commit('changeAddressGoodsSale', addressGoodsSaleResult.data);
      commit('changeTopPanelDatas', topPanelDatasResult.data);
    },
    async getCategoryGoodsCountAction({ commit }) {
      const goodsCountResult = await requestCategoryGoodsCount();
      commit('changeCategoryGoodsCount', goodsCountResult.data);
    },
    async getCategoryGoodsFavorAction({ commit }) {
      const goodsFavorResult = await requestCategoryGoodsSale();
      commit('changeCategoryGoodsFavor', goodsFavorResult.data);
    },
    async getCategoryGoodsSaleAction({ commit }) {
      const goodsSaleResult = await requestCategoryGoodsFavor();
      commit('changeCategoryGoodsSale', goodsSaleResult.data);
    },
    async getAddressGoodsSaleAction({ commit }) {
      const addressGoodsSaleResult = await requestAddressGoodsSale();
      commit('changeAddressGoodsSale', addressGoodsSaleResult.data);
    }
  }
};

export default dashBoardModule;
